package org.apache.rocketmq.broker.example;


import org.apache.rocketmq.store.DefaultMessageStore;
import org.apache.rocketmq.store.SelectMappedBufferResult;
import org.apache.rocketmq.store.logfile.DefaultMappedFile;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class RocketMQMappedFileExample {

    public static void main(String[] args) throws Exception {
        // 必须是数字文件
        DefaultMappedFile mappedFile = new DefaultMappedFile("d:/work/data/0", 1024 * 1024);
        mappedFile.appendMessage(ByteBuffer.wrap("Hello, world!".getBytes(StandardCharsets.UTF_8)));
        int flush = mappedFile.flush(0);
        System.out.println("刷盘到偏移量: " + flush);

        SelectMappedBufferResult selectMappedBufferResult = mappedFile.selectMappedBuffer(0);
        ByteBuffer byteBuffer = selectMappedBufferResult.getByteBuffer();
        byte[] readBytes = new byte[byteBuffer.remaining()];
        byteBuffer.get(readBytes);
        System.out.println("读取数据: " + new String(readBytes, StandardCharsets.UTF_8));
    }
}
